/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-14
 * Time: 9:47
 */
public class Sort {
    /**
     * 选择排序
     * @param array
     */
    public static void selectSort(int[] array){
        for(int i = 0; i < array.length; i++) {
            int minIndex =i;
            for(int j = i + 1; j < array.length; j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, minIndex, i);
        }
    }
    /**
     *
     * 直接插入排序
     */
    public static void insertSort(int[] array){
        // write code  here
        for(int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for(; j >= 0; j--) {
                if(array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    /**
     *
     * 希尔排序
     */
    public static void shellSort(int[] array){
        // write code  here
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }
    }
    public static void shell(int[] array, int gap){
        for(int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for(; j >= 0; j -= gap) {
                if(array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }
    /**
     * 快速排序
     * 好情况下
     * 时间复杂度：O(NlogN)
     * 空间复杂度：O(logN)
     *
     * 最坏情况下 逆序/有序
     * 时间复杂度：O(N^2logN)
     * 空间复杂度：O(N)
     * 稳定性：不稳定
     * @param array
     */
    public static void quickSort(int[] array) {
        int left = 0;
        int right = array.length - 1;

        quick(array, left, right);
    }
    public static void quick(int[] array, int start, int end) {
        if(start >= end) {
            return;
        }
        int pivot = partition(array, start, end);
        quick(array, start, pivot - 1);
        quick(array, pivot + 1, end);
    }
    public static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while(left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, i, left);
        return left;
    }

    /**
     * 挖坑法
     * @param array
     * @param left
     * @param right
     * @return
     */
    public static int partitionHole(int[] array, int left, int right) {
        int tmp = array[left];
        while(left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
}
